package com.ruoyi.project.system.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.OSSClientUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SafetyHazardRecord;
import com.ruoyi.project.system.service.ISafetyHazardRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 安全风险记录Controller  隐患管理
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@RestController
@RequestMapping("/system/safetyHazardRecord")
public class SafetyHazardRecordController extends BaseController
{
    @Autowired
    private ISafetyHazardRecordService safetyHazardRecordService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询安全风险记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SafetyHazardRecord safetyHazardRecord)
    {
        startPage();
        List<SafetyHazardRecord> list = safetyHazardRecordService.selectSafetyHazardRecordList(safetyHazardRecord);
        return getDataTable(list);
    }

    /**
     * 导出安全风险记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "安全风险记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SafetyHazardRecord safetyHazardRecord)
    {
        List<SafetyHazardRecord> list = safetyHazardRecordService.selectSafetyHazardRecordList(safetyHazardRecord);
        ExcelUtil<SafetyHazardRecord> util = new ExcelUtil<SafetyHazardRecord>(SafetyHazardRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 获取安全风险记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(safetyHazardRecordService.selectSafetyHazardRecordById(id));
    }

    /**
     * 新增安全风险记录
     */
    @Log(title = "安全风险记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SafetyHazardRecord safetyHazardRecord)
    {
        return toAjax(safetyHazardRecordService.insertSafetyHazardRecord(safetyHazardRecord));
    }

    /**
     * 修改安全风险记录
     */
    @Log(title = "安全风险记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SafetyHazardRecord safetyHazardRecord)
    {
        return toAjax(safetyHazardRecordService.updateSafetyHazardRecord(safetyHazardRecord));
    }

    /**
     * 删除安全风险记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "安全风险记录", businessType = BusinessType.DELETE)
	@DeleteMapping({"/{ids}/{dealContent}","/{ids}/"})
    public AjaxResult remove(@PathVariable Integer[] ids,@PathVariable(value = "dealContent",required = false) String dealContent)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return toAjax(safetyHazardRecordService.deleteSafetyHazardRecordByIds(ids,dealContent,loginUser.getUser()));
    }


    /**
     * 查询安全风险记录列表(已处理和未处理数据)
     */
    @GetMapping("/getSelectlist")
    public AjaxResult getSelectlist( )
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        List<Map<String,Object>> list = safetyHazardRecordService.getSelectlist(loginUser.getUser());
        return AjaxResult.success(list);
    }

    /**
     * 保存到oss并返回url
     *
     * @return
     */
    @RequestMapping(value = "/picOSS", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult picOSS(@RequestParam("file") MultipartFile file) {

        OSSClientUtil ossClient=new OSSClientUtil();
        String name = ossClient.uploadImg2Oss(file);
        String imgUrl = ossClient.getImgUrl(name);
        String[] split = imgUrl.split("\\?");
        return AjaxResult.success(split[0]);

    }

}
